﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Windows.Forms;
using Transitions;

namespace UIT_CV_Management
{
    public partial class AccountForm : Form
    {
        Boolean invalid = true;
        Boolean isValidOldPassword = false;
        Boolean isValidOldEMPassword = false;
        Boolean isMatchingPassword = false;
        Boolean isMatchingEmail = false;

        public AccountForm()
        {
            InitializeComponent();
            InitLanguage();
        }

        private void lblChangePassword_Click(object sender, EventArgs e)
        {
            lblChangePassword.BackColor = Color.CornflowerBlue;
            lblChangePassword.ForeColor = Color.White;
            lblChangeEmail.BackColor = Color.White;
            lblChangeEmail.ForeColor = Color.Black;

            Control ctrlOnScreen = pnlChangePassword;
            Control ctrlOffScreen = pnlChangeEmail;

            ctrlOnScreen.BringToFront();
            ctrlOffScreen.SendToBack();

            Transition t = new Transition(new TransitionType_EaseInEaseOut(400));
            t.add(ctrlOnScreen, "Left", 3);
            t.add(ctrlOffScreen, "Left", panel1.Width);
            t.run();

            tbxOldPassword.Focus();
        }

        private void lblChangeEmail_Click(object sender, EventArgs e)
        {
            lblChangeEmail.BackColor = Color.CornflowerBlue;
            lblChangeEmail.ForeColor = Color.White;
            lblChangePassword.BackColor = Color.White;
            lblChangePassword.ForeColor = Color.Black;

            Control ctrlOnScreen = pnlChangeEmail;
            Control ctrlOffScreen = pnlChangePassword;

            ctrlOnScreen.BringToFront();
            ctrlOffScreen.SendToBack();

            Transition t = new Transition(new TransitionType_EaseInEaseOut(400));
            t.add(ctrlOnScreen, "Left", 3);
            t.add(ctrlOffScreen, "Left", panel1.Width);
            t.run();

            tbxEMOldPassword.Focus();
        }

        private void InitLanguage()
        {
            this.Text = Language._("ACCOUNT_FORM_TITLE");
            this.lblTitle.Text = Language._("ACCOUNT_FORM_TITLE_LBL");
            this.lblChangePassword.Text = Language._("ACCOUNT_FORM_CHANGE_PASSWORD_LBL");
            this.lblChangeEmail.Text = Language._("ACCOUNT_FORM_CHANGE_EMAIL_LBL");
            this.btnBack.Text = Language._("ACCOUNT_FORM_BACK_BTN");
            this.btnSave.Text = Language._("ACCOUNT_FORM_SAVE_BTN");
            this.lblOldPassword.Text = Language._("ACCOUNT_FORM_OLD_PASSWORD_LBL");
            this.lblEMOldPassword.Text = Language._("ACCOUNT_FORM_OLD_PASSWORD_LBL");
            this.lblNewPassword.Text = Language._("ACCOUNT_FORM_NEW_PASSWORD_LBL");
            this.lblConfirmPassword.Text = Language._("ACCOUNT_FORM_CONFIRM_PASSWORD_LBL");
            this.lblNewEmail.Text = Language._("ACCOUNT_FORM_NEW_EMAIL_LBL");
            this.lblConfirmEmail.Text = Language._("ACCOUNT_FORM_CONFIRM_EMAIL_LBL");
        }

        private void tbxOldPassword_Validating(object sender, CancelEventArgs e)
        {
            if (tbxOldPassword.Text == "" || tbxOldPassword.Text != currentUser.logedinUser.Password)
            {
                tbxOldPassword.BackColor = Color.Tomato;
                utpTip.Value = Language._("ACCOUNT_FORM_WRONG_PASSWORD_TIP");
                isValidOldPassword = false;
            }
            else
            {
                isValidOldPassword = true;
                tbxOldPassword.BackColor = Color.White;
            }
        }

        private void tbxNewPassword_Validating(object sender, CancelEventArgs e)
        {
            if (tbxNewPassword.Text == "")
            {
                tbxNewPassword.BackColor = Color.Tomato;
                utpTip.Value = Language._("ACCOUNT_FORM_MISSING_PASSWORD_TIP");
            }
            else
            {
                tbxNewPassword.BackColor = Color.White;
            }
        }

        private void tbxConfirmPassword_Validating(object sender, CancelEventArgs e)
        {
            if (tbxConfirmPassword.Text == "" || tbxNewPassword.Text != tbxConfirmPassword.Text)
            {
                isMatchingPassword = false;
                tbxConfirmPassword.BackColor = Color.Tomato;
                utpTip.Value = Language._("ACCOUNT_FORM_PASSWORD_NOT_MATCH_TIP");
            }
            else
            {
                isMatchingPassword = true;
                tbxConfirmPassword.BackColor = Color.White;
            }
        }

        private void tbxEMOldPassword_Validating(object sender, CancelEventArgs e)
        {
            if (tbxEMOldPassword.Text == "" || tbxEMOldPassword.Text != currentUser.logedinUser.Password)
            {
                tbxEMOldPassword.BackColor = Color.Tomato;
                utpTip.Value = Language._("ACCOUNT_FORM_WRONG_PASSWORD_TIP");
                isValidOldEMPassword = false;
            }
            else
            {
                isValidOldEMPassword = true;
                tbxEMOldPassword.BackColor = Color.White;
            }
        }

        private void tbxNewEmail_Validating(object sender, CancelEventArgs e)
        {
            if (!IsValidEmail(tbxNewEmail.Text))
            {
                tbxNewEmail.BackColor = Color.Tomato;
                utpTip.Value = Language._("ACCOUNT_FORM_WRONG_EMAIL_TIP");
            }
            else
            {
                tbxNewEmail.BackColor = Color.White;
            }
        }

        private void tbxConfirmEmail_Validating(object sender, CancelEventArgs e)
        {
            if (tbxConfirmEmail.Text == "" || tbxNewEmail.Text != tbxConfirmEmail.Text)
            {
                isMatchingEmail = false;
                tbxConfirmEmail.BackColor = Color.Tomato;
                utpTip.Value = Language._("ACCOUNT_FORM_EMAIL_NOT_MATCH_TIP");
            }
            else
            {
                isMatchingEmail = true;
                tbxConfirmEmail.BackColor = Color.White;
            }
        }

        public bool IsValidEmail(string strIn)
        {
            invalid = false;
            if (String.IsNullOrEmpty(strIn))
                return false;

            // Use IdnMapping class to convert Unicode domain names.
            strIn = Regex.Replace(strIn, @"(@)(.+)$", this.DomainMapper);
            if (invalid)
                return false;

            // Return true if strIn is in valid e-mail format. 
            return Regex.IsMatch(strIn,
                   @"^(?("")(""[^""]+?""@)|(([0-9a-z]((\.(?!\.))|[-!#\$%&'\*\+/=\?\^`\{\}\|~\w])*)(?<=[0-9a-z])@))" +
                   @"(?(\[)(\[(\d{1,3}\.){3}\d{1,3}\])|(([0-9a-z][-\w]*[0-9a-z]*\.)+[a-z0-9]{2,17}))$",
                   RegexOptions.IgnoreCase);
        }

        private string DomainMapper(Match match)
        {
            // IdnMapping class with default property values.
            IdnMapping idn = new IdnMapping();

            string domainName = match.Groups[2].Value;
            try
            {
                domainName = idn.GetAscii(domainName);
            }
            catch (ArgumentException)
            {
                invalid = true;
            }
            return match.Groups[1].Value + domainName;
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            if (isValidOldPassword && isMatchingPassword)
            {
                //Save password
                currentUser.logedinUser.Password = tbxNewPassword.Text;
                currentUser.logedinUser.Save();
            }

            if (isValidOldEMPassword && isMatchingEmail)
            {
                currentUser.logedinUser.Email = tbxNewEmail.Text;
                currentUser.logedinUser.Save();
            }


        }
    }
}
